using RuoVea.OmiApi.UserRoleMenu.Entitys;

namespace RuoVea.OmiApi.UserRoleMenu.Service;

/// <summary>
/// 系统用户角色服务
/// </summary>
public class SysUserRoleService
{
    private readonly SysCacheService _sysCacheService;
    private readonly SugarRepository<SysUserRole> _sysUserRoleRep;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysCacheService"></param>
    /// <param name="sysUserRoleRep"></param>
    public SysUserRoleService(SysCacheService sysCacheService,
        SugarRepository<SysUserRole> sysUserRoleRep)
    {
        _sysCacheService = sysCacheService;
        _sysUserRoleRep = sysUserRoleRep;
    }
    /// <summary>
    /// 授权用户角色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task GrantUserRoles(UserRolesInput input)
    {
        //await _sysUserRoleRep.DeleteAsync(u => u.UserId == input.UserId);
        if (input.RoleIdList == null || input.RoleIdList.Count < 1) return;
        var roles = input.RoleIdList.Select(u => new SysUserRole
        {
            UserId = input.UserId,
            RoleId = u
        }).ToList();
        await _sysUserRoleRep.InsertRangeAsync(roles);
        _sysCacheService.Remove(CacheConst.KeyUserButton + input.UserId);
    }
    /// <summary>
    /// 授权用户角色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task GrantUsersRole(RoleUsersInput input)
    {
        //await _sysUserRoleRep.DeleteAsync(u => u.RoleId == input.RoleId);
        if (input.UserIdList == null || input.UserIdList.Count < 1) return;
        var roles = input.UserIdList.Select(u => new SysUserRole
        {
            UserId = u,
            RoleId = input.RoleId
        }).ToList();
        await _sysUserRoleRep.InsertRangeAsync(roles);
        input.UserIdList.ForEach(userId => {
            _sysCacheService.Remove(CacheConst.KeyUserButton + userId);
        });
    }
    /// <summary>
    /// 根据角色Id删除用户角色
    /// </summary>
    /// <param name="roleId"></param>
    /// <returns></returns>
    public async Task DeleteUserRoleByRoleId(long roleId)
    {
        await _sysUserRoleRep.AsQueryable()
             .Where(u => u.RoleId == roleId)
             .Select(u => u.UserId)
             .ForEachAsync(userId =>
             {
                 _sysCacheService.Remove(CacheConst.KeyUserButton + userId);
             });

        await _sysUserRoleRep.DeleteAsync(u => u.RoleId == roleId);
    }

    /// <summary>
    /// 根据用户Id 角色Id 删除用户角色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task DeleteUserRoleByRoleIdUserIds(RoleUsersInput input)
    {
        await _sysUserRoleRep.AsQueryable()
              .Where(u => u.RoleId == input.RoleId)
              .Select(u => u.UserId)
              .ForEachAsync(userId =>
              {
                  _sysCacheService.Remove(CacheConst.KeyUserButton + userId);
              });

        await _sysUserRoleRep.Context.Deleteable<SysUserRole>().WhereColumns(input.UserIdList.Select(x => new SysUserRole { RoleId = input.RoleId, UserId = x }).ToList(), it => new { it.RoleId, it.UserId }).ExecuteCommandAsync();
    }
    /// <summary>
    /// 根据用户Id删除用户角色
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public async Task DeleteUserRoleByUserId(long userId)
    {
        await _sysUserRoleRep.DeleteAsync(u => u.UserId == userId);
        _sysCacheService.Remove(CacheConst.KeyUserButton + userId);
    }

    /// <summary>
    /// 根据用户Id 角色Id 删除用户角色
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task DeleteUserRoleByUserIdRoleIds(UserRolesInput input)
    {
        await _sysUserRoleRep.Context.Deleteable<SysUserRole>().WhereColumns(input.RoleIdList.Select(x => new SysUserRole { RoleId = x, UserId = input.UserId }).ToList(), it => new { it.RoleId, it.UserId }).ExecuteCommandAsync();
        _sysCacheService.Remove(CacheConst.KeyUserButton + input.UserId);
    }

    /// <summary>
    /// 根据用户Id获取角色集合
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public async Task<List<SysRole>> GetUserRoleList(long userId)
    {
        var sysUserRoleList = await _sysUserRoleRep.AsQueryable()
            .Includes(u => u.SysRole)
            .Where(u => u.UserId == userId).ToListAsync();
        return sysUserRoleList.Where(u => u.SysRole != null).Select(u => u.SysRole).ToList();
    }


    /// <summary>
    /// 根据用户Id获取角色Id集合
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public async Task<List<long>> GetUserRoleIdList(long userId)
    {
        return await _sysUserRoleRep.AsQueryable()
            .Where(u => u.UserId == userId).Select(u => u.RoleId).ToListAsync();
    }

    /// <summary>
    /// 根据角色Id获取用户Id集合
    /// </summary>
    /// <param name="roleId"></param>
    /// <returns></returns>
    public async Task<List<long>> GetUserIdList(long roleId)
    {
        return await _sysUserRoleRep.AsQueryable()
            .Where(u => u.RoleId == roleId).Select(u => u.UserId).ToListAsync();
    }
}